import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-limit-entry',
	templateUrl: './limit-entry.component.html',
	styleUrls: ['./limit-entry.component.css']
})
export class LimitEntryComponent implements OnInit {
	tableData: any = []; // table的表单数据
	totalCounts: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	activeSelected: string = ''; //row高亮
	// 查询参数
	selectedCompanyId: any = ""; // 选中的公司id
	selectedCompanyName: any = ''; // 选中的公司名称
	state: any = ""; // 查询限制进厂的状态
	isOpen: any = ""; // 是否启用
	// searchKey:any=""; // 用户的搜索关键字

	// 标志是否选择车辆的
	isShowDelete: any = true; //是否有删除权限
	// 是编辑还是新增的
	formType: any = 'add';
	selectedCompanyId3: any = '';
	selectedCompanyName3: any = '';
	// 限制进厂记录
	isLimit: any = false;
	// 是否启用
	isCheck: any = true;
	// 表单验证
	validatorOptions = {
		excluded: [':disabled'],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			selectedCompanyName3: {
				trigger:"change",
				validators: {
					notEmpty: {
						message: '请选择公司'
					}
				}
			},
			floatingRange: {
				validators: {
					notEmpty: {
						message: '请填写浮动范围[0-N]'
					},
					regexp: {
						regexp: /^[+-]?(0|([1-9]\d*))(\.\d+)?$/,
						message: '浮动范围[0-N]只能为数字'
					}
				}
			},
			maxTotalMonth: {
				validators: {
					notEmpty: {
						message: '请填写最大总数[30天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: '最大总数[30天]只能为 大于0的数字'
					}
				}
			},
			maxTotalDay: {
				validators: {
					notEmpty: {
						message: '请填写最大总数[每天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: '最大总数[每天]只能为 大于0的数字'
					}
				}
			},
			maxCarMonth: {
				validators: {
					notEmpty: {
						message: '请填写最大记录数[每车30天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: '最大记录数[每车30天]只能为 大于0的数字'
					}
				}
			},
			maxCarDay: {
				validators: {
					notEmpty: {
						message: '请填写最大记录数[每车每天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: '最大记录数[每车每天]只能为 大于0的数字'
					}
				}
			},
			maxCarHostDay: {
				validators: {
					notEmpty: {
						message: '请填写主机问题[每车每天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: '主机问题[每车每天]只能为 大于0的数字'
					}
				}
			},
			maxCarGpsDay: {
				validators: {
					notEmpty: {
						message: '请填写GPS问题[每车每天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: 'GPS问题[每车每天]只能为 大于0的数字'
					}
				}
			},
			maxCarSensorDay: {
				validators: {
					notEmpty: {
						message: '请填写传感器问题[每车每天]'
					},
					regexp: {
						regexp: /^(([1-9][0-9]*(\.)?[0-9]*)|(0(\.)([0-9]*))|(0))$/,
						message: '传感器问题[每车每天]只能为 大于0的数字'
					}
				}
			},
		}
	}
	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}
	ngOnInit() {
		// this.initBtn();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:renewalManage')));
		if (!btnData) {
			return
		};
		for (let item of btnData) {
			if (item.sourceKey === "system:renewal:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}

		}
	}
	load() {
		let param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				companyId: this.selectedCompanyId,
				state: this.state,
				isOpen: this.isOpen,
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("deviceRepaired/config/pushConfigList.html", reqdata).subscribe(result => {
			let data = []
			if (result.isSuccess && result.exhibitDatas) {
				data = result.exhibitDatas
				for (let index = 0; index < data.length; index++) {
					const item = data[index];
					item.floatingRangeStr = item.floatingRange+"["+item.floatingValue+"]"
				}
				this.tableData = result.exhibitDatas
				this.totalCounts = result.recordCount;
			}
		});
	}
	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
	// 分页回调
	paginate(e) {
		this.activeSelected = '';
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 刷新表
	refresh() {
		this.load();
	}
	// 删除续费记录
	del_domCode(row) {
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("deviceRepaired/config/delPushConfig.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	// 表单查询
	tableSearch() {
		this.curPage = 1;
		this.load();
	}


	isScroTopNum: any = 0;  //当用户重新点开添加再进行点击车辆选择时才滚动到头部 其他时候不滚动
	// 限制进厂的配置提交对象
	limitEntryObj: any = {
		companyId: "",
		companyName: "",
		floatingRange: '',
		maxTotalMonth: '',
		maxTotalDay: '',
		maxCarMonth: '',
		maxCarDay: '',
		maxCarHostDay: '',
		maxCarGpsDay: '',
		maxCarSensorDay: '',
		state: '',
		isOpen: ''
	};
	// 

	// 添加续费记录的弹窗
	showAddLimitModal(modal) {
		this.formType = 'add'
		this.isScroTopNum = 0;
		modal.show();
		this.resetForm();
	}
	// 重置添加表
	resetForm() {
		this.limitEntryObj = {
			companyId: "",
			companyName: "",
			floatingRange: '',
			maxTotalMonth: '',
			maxTotalDay: '',
			maxCarMonth: '',
			maxCarDay: '',
			maxCarHostDay: '',
			maxCarGpsDay: '',
			maxCarSensorDay: '',
			state: '',
			isOpen: ''
		};
		this.isCheck = true;
		this.isLimit = false;
		this.selectedCompanyId3 = "";
		this.selectedCompanyName3 = "";
		//初始化表单
		$("#limitEntry").data('bootstrapValidator').resetForm();
	}
	// 关闭添加弹框
	closeLimitEntryModal(modal) {
		$("#limitEntry").data('bootstrapValidator').resetForm();
		modal.hide();
	}
	// 公司选中回调
	factorySelected(data) {
		this.selectedCompanyId3 = data.companyId;
		this.selectedCompanyName3 = data.companyName;
		this.limitEntryObj.companyId = data.companyId;
		this.limitEntryObj.companyName = data.companyName;
		$("#selectedCompanyName3").val(this.limitEntryObj.companyName?this.limitEntryObj.companyName:null).trigger("change")
	}
	// 限于已限制进厂记录
	clickRecord(value) {
		this.limitEntryObj.state = value
		if (value == '1') {
			this.isLimit = false
		} else {
			this.isLimit = true
		}
	}
	clickCheck(value){
		this.limitEntryObj.isOpen = value
		if (value == '1') {
			this.isCheck = true
		} else {
			this.isCheck = false
		}
	}
	// 提交添加结果
	submitModal(modal) {
		$('#limitEntry').bootstrapValidator('validate'); //开启验证
		let isValid = $('#limitEntry').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			this.limitEntryObj.isOpen = this.isCheck ? 1 : 0;
			this.limitEntryObj.state = this.isLimit?2:1
			let reqdata = this.limitEntryObj
			if(!reqdata.floatingDate){
				delete reqdata.floatingDate
			}
			this.jsonApiService.http_obj("deviceRepaired/config/editPushConfig.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					// 
					this.load();
					this.closeLimitEntryModal(modal)
				}
			})
		}
	}
	// 编辑
	editRow(row, modal) {
		modal.show();
		setTimeout(() => {
			this.formType = 'edit'
			this.limitEntryObj = JSON.parse(JSON.stringify(row))
			this.isLimit = this.limitEntryObj.state == 2 ? true : false
			this.isCheck = this.limitEntryObj.isOpen ? true : false;
			// 回显公司名称
			this.selectedCompanyId3 = this.limitEntryObj.companyId;
			this.selectedCompanyName3 = this.limitEntryObj.companyName;
		}, 300);
	}

}